home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / URLClassLoader.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  5.5 KB  |  191 lines

  1. package horst;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.Hashtable;
  9. import java.util.zip.InflaterInputStream;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipException;
  12. import java.util.zip.ZipInputStream;
  13.  
  14. public class URLClassLoader extends ClassLoader {
  15.    private Hashtable classes = new Hashtable();
  16.    private String urlString;
  17.    private char classNameReplacementChar;
  18.    private boolean bReadingJar;
  19.  
  20.    protected String formatClassName(String className) {
  21.       return this.classNameReplacementChar == 0 ? className.replace('.', '/') + ".class" : className.replace('.', this.classNameReplacementChar) + ".class";
  22.    }
  23.  
  24.    public Class loadClass(String className) throws ClassNotFoundException {
  25.       return this.loadClass(className, true);
  26.    }
  27.  
  28.    public synchronized Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException {
  29.       Class result = (Class)this.classes.get(className);
  30.       if (result != null) {
  31.          return result;
  32.       } else {
  33.          try {
  34.             result = super.findSystemClass(className);
  35.             return result;
  36.          } catch (ClassNotFoundException var6) {
  37.             byte[] classBytes = this.loadClassBytes(className);
  38.             if (classBytes == null) {
  39.                throw new ClassNotFoundException();
  40.             } else {
  41.                try {
  42.                   result = ((ClassLoader)this).defineClass(classBytes, 0, classBytes.length);
  43.                   if (result == null) {
  44.                      throw new ClassFormatError();
  45.                   }
  46.                } catch (Exception var5) {
  47.                   throw new ClassFormatError();
  48.                }
  49.  
  50.                if (resolveIt) {
  51.                   System.out.println("resolving : " + result);
  52.                   ((ClassLoader)this).resolveClass(result);
  53.                }
  54.  
  55.                this.classes.put(className, result);
  56.                return result;
  57.             }
  58.          }
  59.       }
  60.    }
  61.  
  62.    protected byte[] loadClassBytes(String className) {
  63.       className = this.formatClassName(className);
  64.       if (this.bReadingJar) {
  65.          return this.readJAREntry(className);
  66.       } else {
  67.          try {
  68.             URL url = new URL(this.urlString + "/" + className);
  69.             URLConnection connection = url.openConnection();
  70.             InputStream inputStream = connection.getInputStream();
  71.             int length = connection.getContentLength();
  72.             byte[] data = new byte[length];
  73.             int numRead = 0;
  74.  
  75.             do {
  76.                try {
  77.                   numRead += inputStream.read(data, numRead, length - numRead);
  78.                } catch (IOException ioe) {
  79.                   System.out.println("URLClassLoader :" + ((Throwable)ioe).getMessage());
  80.                   break;
  81.                }
  82.             } while(numRead < length);
  83.  
  84.             inputStream.close();
  85.             return data;
  86.          } catch (Exception ex) {
  87.             System.out.print("### URLClassLoader.loadClassBytes() - Exception:");
  88.             ((Throwable)ex).printStackTrace();
  89.             return null;
  90.          }
  91.       }
  92.    }
  93.  
  94.    public synchronized void loadJAR(String base, String jarFile) throws Exception {
  95.       this.bReadingJar = true;
  96.       String separator = "/";
  97.       String jarStr = "";
  98.       if (base.endsWith(separator)) {
  99.          jarStr = base + jarFile;
  100.       } else {
  101.          jarStr = base + separator + jarFile;
  102.       }
  103.  
  104.       this.setURLString(jarStr);
  105.       URL u = Utilities.getURL(jarStr);
  106.       if (u != null) {
  107.          try {
  108.             ZipInputStream is = new ZipInputStream(u.openStream());
  109.             ZipEntry zipEntry = null;
  110.  
  111.             while((zipEntry = is.getNextEntry()) != null) {
  112.                String className = zipEntry.getName();
  113.                if (className.endsWith(".class")) {
  114.                   int len = className.length();
  115.                   className = className.substring(0, len - 6);
  116.                   Class result = (Class)this.classes.get(className);
  117.                   if (result == null) {
  118.                      try {
  119.                         super.findSystemClass(className);
  120.                      } catch (ClassNotFoundException var14) {
  121.                         ByteArrayOutputStream out = new ByteArrayOutputStream();
  122.                         int currByte = 0;
  123.  
  124.                         while((currByte = ((InflaterInputStream)is).read()) >= 0) {
  125.                            out.write(currByte);
  126.                         }
  127.  
  128.                         byte[] classBytes = out.toByteArray();
  129.                         System.out.println("className=" + className);
  130.                         result = ((ClassLoader)this).defineClass(classBytes, 0, classBytes.length);
  131.                         if (result != null) {
  132.                            this.classes.put(className, result);
  133.                         }
  134.                      }
  135.                   }
  136.                }
  137.             }
  138.  
  139.             is.close();
  140.          } catch (ZipException var15) {
  141.             System.out.println("ZipException!");
  142.          } catch (IOException var16) {
  143.             System.out.println("IOException!");
  144.          }
  145.       }
  146.  
  147.       this.bReadingJar = false;
  148.    }
  149.  
  150.    byte[] readJAREntry(String className) {
  151.       byte[] classBytes = new byte[0];
  152.  
  153.       try {
  154.          URL u = Utilities.getURL(this.urlString);
  155.          ZipInputStream is = new ZipInputStream(u.openStream());
  156.          ZipEntry zipEntry = null;
  157.  
  158.          while((zipEntry = is.getNextEntry()) != null) {
  159.             String entryName = zipEntry.getName();
  160.             if (entryName.equalsIgnoreCase(className)) {
  161.                ByteArrayOutputStream out = new ByteArrayOutputStream();
  162.                int currByte = 0;
  163.  
  164.                while((currByte = ((InflaterInputStream)is).read()) >= 0) {
  165.                   out.write(currByte);
  166.                }
  167.  
  168.                classBytes = out.toByteArray();
  169.                break;
  170.             }
  171.          }
  172.  
  173.          is.close();
  174.       } catch (ZipException var9) {
  175.          System.out.println("readJAREntry ZipException!");
  176.       } catch (IOException var10) {
  177.          System.out.println("readJAREntry IOException!");
  178.       }
  179.  
  180.       return classBytes;
  181.    }
  182.  
  183.    public void setClassNameReplacementChar(char replacement) {
  184.       this.classNameReplacementChar = replacement;
  185.    }
  186.  
  187.    void setURLString(String url) {
  188.       this.urlString = url;
  189.    }
  190. }
  191.